//
// Created by ziya on 2023/7/9.
//

#ifndef ZIYA_MULTI_CORE_OS_X64_TASK_H
#define ZIYA_MULTI_CORE_OS_X64_TASK_H

#include "types.h"

// 进程上限
#define NR_TASKS 64

typedef void* (*task_fun_t)(void* arg);

typedef enum task_state_t
{
    TASK_INIT,
    TASK_RUNNING,
    TASK_READY,
    TASK_BLOCKED,
    TASK_SLEEPING,
    TASK_WAITING,
    TASK_EXIT,
} task_state_t;

typedef struct 
{
    i64 rflags;
    i64 rip;
    i64 rax;
    i64 rbx;
    i64 rcx;
    i64 rdx;
    i64 rdi;
    i64 rsi;
    i64 rbp;
    i64 rsp;
    i64 r8;
    i64 r9;
    i64 r10;
    i64 r11;
    i64 r12;
    i64 r13;
    i64 r14;
    i64 r15;
    i64 cs;
    i64 ss;
    i64 ds;
    i64 fs;
    i64 gs;
} __attribute__((packed)) task_context_t;

typedef struct task_t 
{
    int pid;
    char name[32];
    task_state_t state;
    task_fun_t function;
    i64 esp0;
    task_context_t context;
    int scheduling_times;
} __attribute__((packed)) task_t;

task_t * task_create(task_fun_t fun, char* name);

void sched();

task_fun_t get_task_function(task_t* task);
task_context_t* get_task_context(task_t* task);

void task_exit(task_t* task);

int inc_scheduling_times(task_t* task);



#endif //ZIYA_MULTI_CORE_OS_X64_TASK_H
